package com.tencent.wepay.service.mchpay;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tencent.wepay.WepayManager;
import com.tencent.wepay.handler.xml.XStreamHandler;
import com.tencent.wepay.listener.Listener;
import com.tencent.wepay.protocol.http.HttpsRequest;
import com.tencent.wepay.protocol.mchpay.domain.Transfer;
import com.tencent.wepay.protocol.mchpay.request.TransferInfoRequest;
import com.tencent.wepay.protocol.mchpay.request.TransferRequest;
import com.tencent.wepay.protocol.mchpay.response.TransferInfoResponse;
import com.tencent.wepay.protocol.mchpay.response.TransferResponse;
import com.tencent.wepay.spi.WepayRepository;

/**
 * 商户付款
 * 
 * @author sunzhenyu
 */
public class TransferService {
	private final static String payUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
	private final static String getPayInfoUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo";
	private static Logger logger = LoggerFactory
			.getLogger(TransferService.class);

	public static TransferResponse pay(Transfer account) {
		WepayRepository repository = WepayManager.getJsapiRepository();
		TransferRequest request = new TransferRequest();
		request.setAppid(repository.getAppid());
		request.setKey(repository.getKey());
		request.setMchId(repository.getMchId());
		try {
			BeanUtils.copyProperties(request, account);
			if (request.validation()) {
				String result = HttpsRequest.getInstance().post(payUrl,request.toXML(), repository);
				return XStreamHandler.toBean(TransferResponse.class, result);
			}
		} catch (Exception e) {
			logger.error("send request error", e);
		}
		return null;
	}

	public static TransferResponse pay(Transfer account,
			Listener<TransferRequest, TransferResponse> listner) {
		WepayRepository repository = WepayManager.getJsapiRepository();
		TransferRequest request = new TransferRequest();
		request.setAppid(repository.getAppid());
		request.setKey(repository.getKey());
		request.setMchId(repository.getMchId());
			try {
				BeanUtils.copyProperties(request, account);
				if (listner != null) {
					listner.beforeSend(request);
				}
				if (request.validation()) {	
					String result = HttpsRequest.getInstance().post(payUrl,request.toXML(), repository);
					TransferResponse response = XStreamHandler.toBean(TransferResponse.class, result);
					if ("SUCCESS".equalsIgnoreCase(response.getReturnCode())
						&& "SUCCESS".equalsIgnoreCase(response.getResultCode())) {
						listner.onSuccess(response);
					} else {
						response.setTradeNo(request.getTradeNo());
						listner.onFail(response);
					}
					return response;
				}

			} catch (Exception e) {
				listner.onError(e);
			}
		return null;
	}

	public static TransferInfoResponse query(String tradeNo) {
		TransferInfoRequest request = new TransferInfoRequest();
		WepayRepository repository = WepayManager.getJsapiRepository();
		request.setAppid(repository.getAppid());
		request.setKey(repository.getKey());
		request.setMchId(repository.getMchId());
		request.setTradeNo(tradeNo);
		if (request.validation()) {
			try {
				String result = HttpsRequest.getInstance().post(getPayInfoUrl,
						request.toXML(), repository);
				System.out.println(result);
				return XStreamHandler
						.toBean(TransferInfoResponse.class, result);
			} catch (Exception e) {
				logger.error("send request error", e);
			}
		}
		return null;
	}
}
